package com.gmcc.docmanager.loader;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import com.gmcc.docmanager.entity.Document;
import com.gmcc.docmanager.entity.DocumentList;
import com.gmcc.docmanager.entity.LibCodeType;

public class DocSearchResponseParser extends ResponseParser {

	public DocSearchResponseParser(ResponseListener listener) {
		super(listener);
	}

	@Override
	public void doParse(String url, HttpResponse httpresp, InputStream is,
			String postData) {
		super.doParse(url, httpresp, is, postData);
		if (netResult && rootElement != null) {
			int errorCode = 1;
			errorCode = stringToInt(getNodeTextValue("errorCode"));
			if (errorCode != 0) {
				response(createFailureResponse(errorCode));
			} else {
				DocumentList documentList = new DocumentList();
				Node documentsNode = getSingleNode("documents");
				if (documentsNode != null) {
					NamedNodeMap nodeMap = documentsNode.getAttributes();
					String unitID = getAttributeValue(nodeMap, "unitID");
					String libCodeStr = getAttributeValue(nodeMap, "libCode");
					int libCode = stringToInt(libCodeStr);
					int pageNum = stringToInt(getAttributeValue(nodeMap,
							"pageNum"));
					int page = stringToInt(getAttributeValue(nodeMap, "page"));
					int totalCount = stringToInt(getAttributeValue(nodeMap,
							"totalCount"));
					documentList.setUnitID(unitID);
					documentList.setLibCode(LibCodeType.valueOf(libCode));
					documentList.setPageNum(pageNum);
					documentList.setPage(page);
					documentList.setTotalCount(totalCount);

					List<Document> documents = new ArrayList<Document>();
					NodeList documentNodeList = rootElement
							.getElementsByTagName("document");
					if (documentNodeList != null) {
						for (int i = 0; i < documentNodeList.getLength(); i++) {
							Node documentNode = documentNodeList.item(i);
							NamedNodeMap documentNodeMap = documentNode
									.getAttributes();
							Document document = new Document();
							document.setDocID(getAttributeValue(
									documentNodeMap, "docID"));
							document.setRn(stringToInt(getAttributeValue(
									documentNodeMap, "rn")));

							for (Node documentChildNode = documentNode
									.getFirstChild(); documentChildNode != null; documentChildNode = documentChildNode
									.getNextSibling()) {
								if (documentChildNode.getNodeType() == Node.ELEMENT_NODE) {
									if ("title".equals(documentChildNode
											.getNodeName())) {
										document.setTitle(getNodeTextValue(documentChildNode));
									} else if ("createtime"
											.equals(documentChildNode
													.getNodeName())) {
										document.setCreatetime(getNodeTextValue(documentChildNode));
									} else if ("mj".equals(documentChildNode
											.getNodeName())) {
										document.setMj(getNodeTextValue(documentChildNode));
									} else if ("bgqx".equals(documentChildNode
											.getNodeName())) {
										document.setBgqx(getNodeTextValue(documentChildNode));
									} else if ("zrz".equals(documentChildNode
											.getNodeName())) {
										document.setZrz(getNodeTextValue(documentChildNode));
									} else if ("gdrq".equals(documentChildNode
											.getNodeName())) {
										document.setGdrq(getNodeTextValue(documentChildNode));
									} else if ("wenhao"
											.equals(documentChildNode
													.getNodeName())) {
										document.setWenhao(getNodeTextValue(documentChildNode));
									} else if ("keyword"
											.equals(documentChildNode
													.getNodeName())) {
										document.setKeyword(getNodeTextValue(documentChildNode));
									}
								}
							}
							documents.add(document);
						}
					}
					documentList.setDocuments(documents);
				}

				Response response = new Response();
				response.setCode(errorCode);
				response.setResponseObject(documentList);
				response(response);
			}
		}
	}
}
